/*
 * Copyright 2010 Christoph Widulle (christoph.widulle@googlemail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
 * License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS"
 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */



package com.android1.amarena2d.animation;

import com.android1.amarena2d.engine.EngineObject;
import com.android1.amarena2d.texture.AtlasTextureFrame;
import com.android1.amarena2d.texture.TextureAtlas;
import com.android1.amarena2d.texture.TextureFrame;

import java.util.ArrayList;
import java.util.List;

public class AtlasAnimationBuilder extends EngineObject {

    TextureAtlas textureAtlas;
    ArrayList<TextureFrame> textureFrames = new ArrayList<TextureFrame>(30);
    String name;
    float delay;

    AtlasAnimationBuilder() {
    }

    AtlasAnimationBuilder init(TextureAtlas textureAtlas, float delay) {
        this.textureAtlas = textureAtlas;
        this.delay = delay;
        this.name = null;
        this.textureFrames.clear();
        return this;
    }

    public AtlasAnimationBuilder(TextureAtlas textureAtlas, float delay) {
        this.textureAtlas = textureAtlas;
        this.delay = delay;
    }

    public AtlasAnimationBuilder addFrame(AtlasTextureFrame frame) {
        if (frame.getTextureAtlas() != this.textureAtlas)
            throw new IllegalArgumentException("Frame ist not from same TextureAtlas.");
        textureFrames.add(frame);
        return this;
    }

    public AtlasAnimationBuilder addFrames(int... atlasFrameIndex) {
        for (int i = 0; i < atlasFrameIndex.length; i++) {
            textureFrames.add(textureAtlas.getTextureFrame(atlasFrameIndex[i]));
        }
        return this;
    }

    public AtlasAnimationBuilder addFrame(int atlasFrameIndex) {
        textureFrames.add(this.textureAtlas.getTextureFrame(atlasFrameIndex));
        return this;
    }

    public AtlasAnimationBuilder name(String name) {
        this.name = name;
        return this;
    }

    public AtlasAnimationBuilder delay(float delay) {
        this.delay = delay;
        return this;
    }

    public AtlasAnimation create() {
        if (textureFrames.size() == 0) {
            final List<AtlasTextureFrame> allframe = textureAtlas.getTextureFrames();
            for (int i = 0; i < allframe.size(); i++) {
                AtlasTextureFrame atlasTextureFrame = allframe.get(i);
                addFrame(atlasTextureFrame);
            }
        }
        Animation.Frame[] frames = new Animation.Frame[textureFrames.size()];
        for (int i = 0; i < textureFrames.size(); i++) {
            TextureFrame textureFrame = textureFrames.get(i);
            frames[i] = new Animation.FrameImpl(textureFrame);
        }
        return new AtlasAnimation(textureAtlas, name, delay, frames);
    }
}